//把日期转化为时间戳数字
function timeTampTransform(time) {
  var dt = new Date(time.replace(/-/g, "/"));
  return dt.getTime();
}

// 和当前时间的差距,返回时间
function timeToNow(time: string) {
  var dt = new Date(time.replace(/-/g, "/"));
  const targetTime = dt.getTime();
  const now = new Date().getTime()
  return targetTime - now
}

function timeNumber(time: string) {
  var dt = new Date(time.replace(/-/g, "/"));
  const targetTime = dt.getTime();
  return targetTime
}

function restTime(time: string) {
  if (!time) {
    return {
      day: 0,
      hour: 0,
      minute: 0,
      sec: 0
    }
  }

  const restSec = timeToNow(time)

  if (restSec < 0) {
    return {
      day: 0,
      hour: 0,
      minute: 0,
      sec: 0
    }
  }

  // @ts-ignore
  const day = parseInt(restSec / (60 * 60 * 24 * 1000))
  // @ts-ignore
  const hour = parseInt(restSec / (60 * 60 * 1000) % 24)
  // @ts-ignore
  const minute = parseInt(restSec / (60 * 1000) % 60)
  // @ts-ignore
  const sec = parseInt(restSec / 1000 % 60)

  return {
    day,
    hour,
    minute,
    sec
  }
}

export const CommonTime = {
  timeTampTransform,
  timeToNow,
  restTime,
  timeNumber
};

